import numpy as np
import cv2
from utils import nms

class SpecialRegion:
    def __init__(self, contour):
        self.bbox = cv2.boundingRect(contour)
        self.points = []
        for c in contour:
            inside_nx = (c[0][0]-self.bbox[0])/self.bbox[2]#nomalized x coordinate inside
            inside_ny = (c[0][1]-self.bbox[1])/self.bbox[3]
            self.points.append([inside_nx, inside_ny])
        self.contour = contour
        # self.next=None
        # self.previous=None
        # self.child=None
        # self.parent=None

    def interate(self, specialRegion):
        i, bInclude = self.iou(specialRegion)
        if bInclude==2 or (bInclude == 0 and i >= 0.6):
            if not self.child:
                self.child = specialRegion
                specialRegion.parent = self
            else:
                the_child = self.child
                while the_child.next is not None:
                    the_child = the_child.next
                the_child.next = specialRegion
                specialRegion.previous = the_child
                specialRegion.parent = self
        elif bInclude == 1:
            if not specialRegion.child:
                specialRegion.child = self
                self.parent = specialRegion
            else:
                the_child = specialRegion.child
                while the_child.next is not None:
                    the_child = the_child.next
                the_child.next = self
                self.previous = the_child
                self.parent = specialRegion
        else:
            if not self.next:
                self.next = specialRegion
                specialRegion.previous = self
            else:
                the_next = self.next
                while the_next.next is not None:
                    the_next = the_next.next
                the_next.next = specialRegion
                specialRegion.previous = the_next

    def iou(self, specialRegion):
        ix = max(self.bbox[0], specialRegion.bbox[0])#intersection x
        iy = max(self.bbox[1], specialRegion.bbox[1])
        ix1 = min(self.bbox[0]+self.bbox[2], specialRegion.bbox[0]+specialRegion.bbox[2])
        iy1 = min(self.bbox[1]+self.bbox[3], specialRegion.bbox[1]+specialRegion.bbox[3])
        iw = ix1-ix
        ih = iy1-iy
        if iw<=0 or ih <=0:
            return 0, 0
        else:
            if iw*ih == self.bbox[2]*self.bbox[3]:
                return (iw*ih)/(self.bbox[2]*self.bbox[3]+specialRegion.bbox[2]*specialRegion.bbox[3]-iw*ih), 1
            elif iw*ih == specialRegion.bbox[2]*specialRegion.bbox[3]:
                return (iw*ih)/(self.bbox[2]*self.bbox[3]+specialRegion.bbox[2]*specialRegion.bbox[3]-iw*ih), 2
            else:
                return (iw * ih) / (
                            self.bbox[2] * self.bbox[3] + specialRegion.bbox[2] * specialRegion.bbox[3] - iw * ih), 0

    def bRectStandForAllPoints(self):
        count = 0
        for point in self.points:
            if (point[0] < 0.2 or point[0] > 0.8) and (point[1] < 0.2 or point[1] > 0.8):
                count+=1
        if count/len(self.points)>0.95:
            return True
        else:
            return False

    def judge_matchSR(self, right, thresh):
        if abs(self.bbox[2]/right.bbox[2]-1) < thresh and abs(self.bbox[3]/right.bbox[3]-1) < thresh and abs(self.bbox[2]*right.bbox[3]/self.bbox[3]/right.bbox[2]-1)<thresh:
            return True
        else:
            return False

class SpecialRegions:
    def __init__(self, contours):
        bboxes = []
        srs = []
        for contour in contours:
            sr = SpecialRegion(contour)
            bboxes.append(sr.bbox)
            srs.append(sr)
        bboxes = np.array(bboxes)
        self.nms_bbox = bboxes[nms(bboxes)]
        for nms_contours in self.nms_contours:
            pass